const app = getApp()
const srcApi = app.globalData.srcApi
Page({
  onShareAppMessage: function () {
    return {
      title: app.globalData.app,
      path: '/pages/service/service'
    }
  },
  data: {
    formData:[
    ],
    banner: '',
    appid:''
  },
  onLoad: function () {
    let _this=this
    this.data.appid = app.globalData.appid
    wx.setNavigationBarTitle({
      title: '获取服务'
    })
    //判断授权信息
    if (!app.globalData.info){
      wx.showModal({
        title:'授权信息',
        content: app.globalData.app+'需要获取您的授权',
        success:function(res){
          if(res.confirm){
            wx.openSetting({
              success: function (res) {
                if (res.authSetting["scope.userInfo"]) {
                  wx.getUserInfo({
                    success: function (res) {
                      wx.request({
                        url: app.globalData.api + '/session_key?appid=' + _this.data.appid + '&session_key=' + app.globalData.key,
                        method: 'POST',
                        data: {
                          data: res
                        },
                        success: function (res) {
                          console.log("授权成功！！！")
                          //授权成功
                          app.globalData.info = true
                        }
                      })
                    }
                  })
                }
              }
            })
          }
        }
      })
      
    }
    
    wx.request({
      url: app.globalData.api + '/get_service?appid=' + _this.data.appid + '&session_key=' + app.globalData.key,
      method:'GET',
      success: function (res) {
        if (!res.data.error_code){
          let data=res.data.data
          data.map(function(x){
            if(x.types=='input'){
              x.content=''
            }else if(x.types=='radio'){
              x.checked=x.content[0].name
            }else if(x.types=='checkbox'){
              x.checked={}
            }
          })
          _this.setData({
            formData: data
          })
        }
      },
      fail:function(){
        wx.showToast({
          title: '加载失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
    wx.request({
      url: app.globalData.api + '/get_service_banner?appid=' + _this.data.appid + '&session_key=' + app.globalData.key,
      success:function(res){
        let data=res.data.data
        _this.setData({
          banner:srcApi+data.img
        })
      }
    })
  },
  //radio点击事件
  formClicked:function(e){
    var index=e.currentTarget.dataset.index
    var val = e.currentTarget.dataset.val
    var formData=this.data.formData
    formData[index].checked=val.name
    this.setData({
      formData: formData
    })
  },
  //checkbox点击事件
  checkboxClicked:function(e){
    let index = e.currentTarget.dataset.index
    let val = e.currentTarget.dataset.val.name
    let formData = this.data.formData
    formData[index].checked[val] = !formData[index].checked[val]
    this.setData({
      formData: formData
    })
  },
  formSubmit:function(e){
    let _this=this
    let formData =this.data.formData
    let data=[]
    for(let i in formData){  
      if (!e.detail.value[formData[i].id]){
        wx.showToast({
          title: formData[i].name + '为空！',
          icon: 'none',
          duration: 1000
        })
        return 0
      }
    }
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.api + '/submit' + '?session_key=' + app.globalData.key + '&appid=' + _this.data.appid,
      data: {
        data: e.detail.value,
        orgin: app.globalData.route,
      },
      method: 'POST',
      success: function (res) {
        if(res){
          wx.showToast({
            title: '提交成功！',
            icon: 'success',
            duration: 2000,
            success:function(){
              wx.navigateBack()
            }
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '提交失败！',
          duration: 1000
        })
      }
    })
  }
})